/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Grid = require("grid");
	var store =require("store");
	var activeUser = store.get("user");
	
    var instance = {
        initComponent: function (params,widget) {
        	var grid = new Grid({
        		parentNode: ".J-grid",
        		//autoRender:false,
        		model: {
        			url: "api/organizationsettlement/querybyservice",
        			params: function(){
        				var subnav = widget.get("subnav");
        				var rangeDate = subnav.getValue("startDate");
        				return {
        					"pkOrganization":activeUser.organization.pkOrganization, 
        					"startDate":rangeDate.start,
        	            	"endDate":rangeDate.end,
        	            	fetchProperties:"*,pkOrganizationSettlement,paymentType,content,expectedAmount,expectedDate,paymentMethod," +
        	            			"isInvoice,invoiceDate,description,status,version,alliInfo,orgInfo,pkOrganization,pkAllianceOrg"
        				};
        			},
        			columns:[{
        				 name: "paymentType",
        	             label: "款项类型",
        			},{
        				 name: "content",
        	             label: "款项内容"
        			},{
        				 name: "expectedAmount",
        	             label: "金额"
        			},{
        				 name: "alliInfo",
        	             label: "付款单位",
        	             format:function(value,data){
        	            	 if(data.paymentType == "预收"){
        	            		 return data.alliInfo;
        	            	 }else{
        	            		 return data.orgInfo;
        	            	 }
        	             }
        			},{
        				 name: "orgInfo",
        	             label: "收款单位",
        	             format:function(value,data){
        	            	 if(data.paymentType == "预付"){
        	            		 return data.alliInfo;
        	            	 }else{
        	            		 return data.orgInfo;
        	            	 }
        	             }
        			},{
        				 name: "expectedDate",
        	             label: "收/付款日期",
        	             format: "date"
        			},{
        				name: "show",
       	                label: "操作",
       	                format: "button",
       	                formatparams: [{
       	                	id: "edit",
       	                	text: "修改 ",
       	                	handler: function(index,data,rowEle){
       	                		var form  = widget.get("form");
       	                		widget.get("subnav").hide(["add","startDate"]).show(["return"]);
        						widget.hide([".J-grid"]).show([".J-form"]);
        						form.setData(data);
        						form.getPlugin("paymentType").setText(data.paymentType);
        						form.getPlugin("paymentMethod").setText(data.paymentMethod);
        						form.setValue("content",data.content);
        						form.setValue("expectedAmount",data.expectedAmount);
        						form.setValue("isInvoice",data.isInvoice+"");
        						form.setValue("invoiceDate",data.invoiceDate);
        						form.setValue("description",data.description);
        						
      						    //form.setData("alliInfo",[{pkAllianceOrg:data.pkAllianceOrg,name:data.alliInfo}]);
   	                		    //form.setValue("alliInfo",data.alliInfo);
        						form.getPlugin("alliInfo").setText(data.alliInfo);
     	                		form.setValue("orgName",data.orgInfo);
     	                		form.setValue("status",data.status);
     	                		form.setValue("version",data.version);
     	                		form.setValue("pkOrganizationSettlement",data.pkOrganizationSettlement);
     	                		form.setValue("orgInfo",data.pkOrganization);
     	                		form.setDisabled(false);
//        						form.load("payer",{
//        			                params : {
//        								fetchProperties:"pkAllianceOrg,name"
//        							},
//        			                callback : function(datas){
//        			                	form.setData("payer",datas);
//        			                	form.setValue("payer",data.pkPayer);
//        			              }
//        			            });
//        						form.load("payee",{
//        			                params : {
//        								fetchProperties:"pkAllianceOrg,name"
//        							},
//        			                callback : function(datas){
//        			                	form.setData("payee",datas);
//        			                	form.setValue("payee",data.pkPayee);
//        			              }
//        			            });
       	                	}
       	                },{
       	                	id: "detail",
       	                	text: "查看",
       	                	handler: function(index,data,rowEle){
       	                		var form  = widget.get("form");
       	                		form.reset();
       	                		form.setData({
       	                			pkOrganizationSettlement:data.pkOrganizationSettlement,
       	                			content:data.content,
       	                			expectedAmount:data.expectedAmount,
       	                			expectedDate:data.expectedDate,
       	                			isInvoice:data.isInvoice,
       	                			invoiceDate:data.invoiceDate,
       	                			description:data.description,
       	                		});
       	                		form.getPlugin("paymentType").setText(data.paymentType);
       	                		form.getPlugin("paymentMethod").setText(data.paymentMethod);
       	                		form.setValue("isInvoice",data.isInvoice+"");
       	                	   // form.setData("alliInfo",[{pkAllianceOrg:data.pkAllianceOrg,name:data.alliInfo}]);
       	                		form.getPlugin("alliInfo").setText(data.alliInfo);
       	                	    //form.setValue("alliInfo",data.alliInfo);
  	                		    form.setValue("orgInfo",data.pkOrganization);
       	                		form.setDisabled(true);
       	                		widget.get("subnav").hide(["add"]).show(["return"]);
        						widget.hide([".J-grid"]).show([".J-form"]);
       	                	}
       	                }]
        			}]
        		}
        	});
        	widget.set("grid", grid);
        }
    };
    module.exports = instance;
});
